<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>考勤管理</el-breadcrumb-item>
      <el-breadcrumb-item>出勤表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input placeholder="申请人" clearable v-model="empName" @clear="searchEmpName">
            <el-button slot="append" icon="el-icon-search" @click="searchEmpName"></el-button>
          </el-input>
        </el-col>

        <el-col :span="1">
          <el-button type="primary" @click="addAttendanceFormVisible=true">添加考勤记录</el-button>
        </el-col>
      </el-row>

      <el-table :data="attendanceList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="员工编号" prop="eid" ></el-table-column>
        <el-table-column label="员工姓名" prop="empName" ></el-table-column>
        <el-table-column label="记录时间" prop="recordingTime" ></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope" >
<!--            <el-button type="warning" size="mini" @click="showEditDialog(scope.row)">编辑</el-button>-->
            <el-button type="danger" size="mini" @click="removeById(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 4 , 8, 16]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      ></el-pagination>
    </el-card>
    <div>
      <el-dialog title="添加考勤" :visible.sync="addAttendanceFormVisible" :close-on-click-modal="false">
        <el-form ref="addChannelFormRef" :rules="addAttendanceFormRules" :model="addAttendanceForm" label-width="80px" size="mini">

          <el-form-item label="员工工号" prop="eId">
            <el-input v-model="addAttendanceForm.eId"></el-input>
          </el-form-item>
          <el-form-item label="记录时间" prop="recordingTime">
            <el-input v-model="addAttendanceForm.recordingTime"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancelAddAttendanceDialog">取 消</el-button>
          <el-button type="primary" @click="addAttendance">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  name: "attendance",
  data(){
    return{
      eId:'',
      empName:'',
      attendanceList:[],
      pageNum:1,
      pageSize:2,
      total:0,
      addAttendanceFormVisible:false,
    //添加考勤表单
      addAttendanceForm:{
        eId:'',
        recordingTime:'',
      },
      addAttendanceFormRules:{

      },
    }
  },
  methods:{
    searchEmpName(){
      this.initAttendance();
    },
    showEditDialog(row){
      this.modifyAttendanceFrom = row;
      this.addAttendanceFormVisible = true;
    },
    handleSizeChange(size){
      this.pageSize = size;
      this.initAttendance();
    },
    handleCurrentChange(now){
      this.pageNum = now;
      this.initAttendance();
    },
    removeById(id){
      let url = this.$HRMSUrl+"/attendance/del/"+id;
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.delete(url)
            .then(res=>{
              if (res.data.code == 200){
                this.initAttendance()
              }else {
                this.$message({
                  type:"error",
                  message:res.data.msg,
                  duration:1000
                })
              }
            })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    addAttendance(){
      let url = this.$HRMSUrl + "/attendance/add";
      this.$axios.post(url,this.addAttendanceForm).then(
          res=>{
            if (res.data.code == 200){
              this.addAttendanceFormVisible = false;
              this.addAttendanceForm = {};
              this.initAttendance();
            }else {
              this.$message({
                type:"error",
                message:res.data.msg,
                duration:1000
              })
            }
          }).catch(error=>{
        this.$message({
          type:"error",
          message:"系统错误，请联系管理员!",
          duration:1000
        })
      })
    },
    initAttendance(){
      let url = this.$HRMSUrl + "/attendance/attendanceList";
      console.log(this.empName)
      this.$axios.get(url,{
        params:{
          empName:this.empName,
          now:this.pageNum,
          size:this.pageSize
        }
      }).then(res=>{
        if (res.data.code == 200){
          console.log(res.data.data.list)
          this.attendanceList = res.data.data.list;
          this.pageNum = res.data.data.pageNum;
          this.pageSize = res.data.data.pageSize;
          this.total = res.data.data.total;
        }else {
          this.$message({
            type:"error",
            message:res.data.msg,
            duration:1000
          })
        }
      }).catch(error=>{
        this.$message({
          type:"error",
          message:"系统错误，请联系管理员！",
          duration:1000
        })
      })
    },
    cancelAddAttendanceDialog(){
      this.addAttendanceForm={};
      this.addAttendanceFormVisible=false;
    },
  },

  created() {
    this.initAttendance();
  },
}
</script>

<style scoped>

</style>